<?php
namespace app\index\controller;

use app\common\entity\Image;
use app\common\entity\User;
use app\common\shop\HomeCateModel;
use app\common\shop\ShopGoodsModel;
use app\common\entity\GoodsCategory;
use app\common\shop\ShopGroupModel;
use app\common\shop\SpecListModel;
use app\common\shop\SpecModel;
use think\Db;
use think\Request;
use think\Controller;

class Shopgoods extends Common {
    public function initialize()
    {
        header("Access-Control-Allow-Origin:*");
        header("Access-Control-Allow-Methods:GET, POST, OPTIONS, DELETE");

        header("Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, Accept-Language, Origin, Accept-Encoding,Accept,Connection,Cookie,token,Access-Token,PHPSESSID,referer,host,content-length");
    }
    /**
     * [首页商品]
     * @param
     * @return
     */
    public function homegoods( Request $request ){
        $homecate = HomeCateModel::field('id,img')->order('id asc')->select()->toArray();

        foreach ($homecate as $key => $value) {
            $goods = ShopGoodsModel::field('id,path,title,price,shoptype')
                    ->where('homecate',$value['id'])
                    ->limit(4)
                    ->order('sort asc')
                    ->select()
                    ->toArray();
            foreach ($goods as $k => $val) {
                $goods[$k]['img'] = unserialize($val['path'])[0];
            }
            $homecate[$key]['goods'] = $goods;
            $homecate[$key]['img'] = unserialize($value['img']);
        }
        return json(['code' => 0, 'msg'=>'请求成功','info'=>$homecate]);
    }
    /**
     * [轮播图]
     * @param
     * @return
     */
    public function image( Request $request ){
        $img = Image::where('types', 1)->order('sort desc,id desc')->select()->toArray();
        return json(['code' => 0, 'msg' => '请求成功','info'=>$img]);
    }
    /**
     * [商品信息-列表]
     * @param
     * @return
     */
    public function goodsList( Request $request ){
        $goods = ShopGoodsModel::field('id,title,cateid,price,total,path,status,shoptype');
        $where = [];
        $page = $request->post('page',1);
        $limit = $request->post('limit',20);
        if($cateid = $request->post('cateid')){
            //$where['cateid'] = $cateid;
            $goods->where('cateid',$cateid);
        }
        if($shoptype = $request->post('shoptype')) {
            //$where['shoptype'] = $shoptype;
            $goods->where('shoptype',$shoptype);
        }
        if($title = $request->post('title')) {
            //$where['title'] = ['like','%'.$title.'%'];
            $goods->where('title','like','%'.$title.'%');
        }

        if($homecate = $request->post('homecate')) {
            //$where['shoptype'] = $shoptype;
            $goods->where('homecate',$homecate);
        }
        $list = $goods
            ->where('status',1)// 上架才显示
            ->where($where)
            ->page($page,$limit)
            ->order('sort asc')
            ->select()
            ->toArray();// 转为数组
        foreach ($list as $key => $value) {
            $path = unserialize($value['path']);
            $list[$key]['path'] = $path;
            $list[$key]['img'] = $path[0];
        }
        return json(['code' => 0, 'msg' => '请求成功','info'=>$list]);
    }

    /**
     * [商品详情]
     * @param
     * @return
     */
    public function goodsDetail( Request $request ){
        $goodsid = $request->post('goodsid',1);// 商品id
        $goods = ShopGoodsModel::getGoods($goodsid);
        if(empty($goods)) return json(['code'=>1,'msg'=>'商品不存在']);
        $goods = $goods->toArray();
        $goods['path'] = unserialize($goods['path']);
        $specList =  SpecListModel::getList($goodsid)->toArray();// 规格列表
        if($specList){
            // 规格
            $list = SpecModel::getGoodsSpec($specList);
            $goods['speclist'] = $list['spec'];
            $spec_title = SpecModel::indexGoodsSpec($specList);
            $goods['spec_title'] = $spec_title;
        }

        return json(['code'=>0,'msg'=>'请求成功','info'=>$goods]);
    }

    /**
     * [正在拼团信息]
     * @param
     * @return
     */
    public function grouplist( Request $request ){
        $page = $request->post('page',1);
        $limit = $request->post('limit',100);
        $goodsid = $request->post('goodsid');
        $list = ShopGroupModel::alias('sg')
            ->field('sg.id,sg.number,sg.havenumber,sg.overtime,sg.createtime,u.avatar,u.nick_name')
            ->leftJoin('user u','u.id = sg.uid')
            ->where('sg.status',0)
            ->where('sg.goodsid',$goodsid)
            ->page($page,$limit)
            ->select()->toArray();
        foreach ($list as $key => $value) {
            $list[$key]['need'] = $value['number'] - $value['havenumber'];
            $list[$key]['over'] = $value['overtime'] - time();
        }
        return json(['code' => 0, 'msg'=>'请求成功','info'=>$list]);
    }

    /**
     * [规格信息]
     * @param
     * @return
     */
    public function getSpecList( Request $request ){
        $key = $request->post('key');
        $goodsid = $request->post('goodsid');
        $price = SpecListModel::getSpecList($key,$goodsid);
        return json(['code' => 0, 'msg' => '请求成功','info'=>$price]);
    }

    /**
     * [获取分类列表]
     * @return
     */
    public function getCateList(){
        $shoptype = input('shoptype');
        $where = [];
        if($shoptype){
            $where['shoptype'] = $shoptype;
        }
        $catelist = GoodsCategory::getAllCate($where)->toArray();
        return json(['code'=>0,'msg'=>'请求成功','info'=>$catelist]);
    }


}